home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / viewwnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-12-21  |  9.8 KB  |  423 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // ViewWnd.cpp : implementation file
  12. // This class perform preview of External object.
  13.  
  14. // -----------  CreatePreview() function -----------------
  15. // Create new drawing that we use to display ExternalObject
  16. // Currently there is not possibility to create new drawing with TurboCAD's SDK in way
  17. // that this drawing was invisible for user
  18. // So we have to use TurboCAD's engine functions to create new drawing that we use to display external objects
  19. // This function is called one time when tool is started to setup preview drawing
  20. // -----------  DoPreview() function ---------------------
  21. // Display External object selected in Objects list in Preview window
  22.  
  23.  
  24.  
  25.  
  26. #include "stdafx.h"
  27. #include "InsSmObj.h"
  28. #include "ViewWnd.h"
  29. #include "SdiDialog.h"
  30.  
  31.  
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CPreviewWnd CViewWnd
  40. const long GF_COSMETIC = 128;
  41. const long GF_ALL = 0;
  42.  
  43. CPreviewWnd::CPreviewWnd()
  44. {
  45.     m_FirstStart = TRUE;
  46.     m_pIApp = NULL;
  47.     m_pDrawCol = NULL;
  48.     m_pPreviewDrawing = NULL;
  49.     m_pPreviewGrs = NULL;
  50.     m_pPreviewView = NULL; 
  51.     m_pViewsCol = NULL;
  52.     m_pPreviewGraphic = NULL;
  53.  
  54. }
  55.  
  56. CPreviewWnd::~CPreviewWnd()
  57. {
  58. }
  59.  
  60.  
  61. BEGIN_MESSAGE_MAP(CPreviewWnd, CWnd)
  62.     //{{AFX_MSG_MAP(CPreviewWnd)
  63.     ON_WM_PAINT()
  64.     ON_WM_ERASEBKGND()
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CPreviewWnd message handlers
  71.  
  72. void CPreviewWnd::OnPaint() 
  73. {
  74.     CPaintDC dc(this); // device context for painting
  75.     
  76.     if (m_pPreviewView == NULL)
  77.         return;
  78.  
  79.     // Force redraw of view window
  80.     try
  81.     {
  82.         HRESULT hRes = m_pPreviewView->Refresh();
  83.                 CHECK_HRESULT(hRes)
  84.  
  85.  
  86.     }
  87.     catch (...)
  88.     {
  89.         TRACE_EXCEPTION("CPreviewWnd::OnPaint")
  90.     }
  91.     
  92. }
  93.  
  94. BOOL CPreviewWnd::OnEraseBkgnd(CDC* pDC) 
  95. {
  96.     CRect rect;
  97.     GetClientRect(rect);
  98.     pDC->FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));
  99.     return TRUE;
  100. }
  101.  
  102.  
  103.  
  104. void CPreviewWnd::CreatePreview() //create a preview drawing
  105. {
  106.     HRESULT hRes;
  107.     COleVariant varType((const long)imsiPolyline);
  108.     COleVariant varOptional(varMissing);
  109.     
  110.     try
  111.     {    
  112. //  protect from run this function several times
  113. // we create preview drawing one time just after tool is runned
  114. // after that we call DoPreview() function to display External Object (selected in the Objects list)
  115. // in Preview window (IDC_PREVIEW)
  116.         if (m_FirstStart == TRUE)
  117.         {
  118.             ClearAll();
  119.             HMODULE hDBAPI = NULL;
  120.             hDBAPI = GetModuleHandle(DBAPI14);
  121.             if (hDBAPI == NULL)
  122.             {
  123.                 hDBAPI = GetModuleHandle(DBAPI12);
  124.                 if (hDBAPI == NULL)
  125.                 {
  126.                     hDBAPI = GetModuleHandle(DBAPI11);
  127.                     if (hDBAPI == NULL)
  128.                     {
  129.                         hDBAPI = GetModuleHandle(DBAPI10);
  130.                         if (hDBAPI == NULL)
  131.                         {
  132.                             hDBAPI = GetModuleHandle(DBAPI90);
  133.                             if (hDBAPI == NULL)
  134.                             {
  135.                                 hDBAPI = GetModuleHandle(DBAPI80);
  136.  
  137.                                 if (hDBAPI == NULL)
  138.                                 {
  139.                                     hDBAPI = GetModuleHandle(DBAPI70);
  140.  
  141.                                     if (hDBAPI == NULL)
  142.                                     {
  143.                                         hDBAPI = GetModuleHandle(DBAPI60);
  144.                                     }        
  145.                                 }
  146.                             }
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.             if (hDBAPI == NULL)
  152.                 return;
  153.             else 
  154.                 m_pfGetApp = (APPGETCURRENTAPP)GetProcAddress(hDBAPI, _T("AppGetCurrentApp"));
  155.                 if (m_pfGetApp == NULL)                                                    
  156.                 return;
  157.  
  158.  
  159.             m_lApp = (*m_pfGetApp)();
  160.             if (m_lApp == 0)
  161.             return;
  162.             
  163.             
  164.             m_pfAppDwgNew = (APPDRAWINGNEWEX)GetProcAddress(hDBAPI, _T("AppDrawingNewEx"));
  165.             if (m_pfAppDwgNew == NULL)                                                    
  166.                 {
  167.                     AfxMessageBox("m_pfAppDwgNew ==    NULL");//# Non-localizable string#
  168.                 
  169.                 }
  170.  
  171.             BOOL i;
  172.             i = TRUE;
  173. // Create a new drawing with TurboCAD's engine call that we use for preview of External objects
  174.             m_lDwg = (*m_pfAppDwgNew)(m_lApp, i, NULL, NULL, true);
  175.             
  176.             COleVariant varItem = (0L);
  177.             long nDrawings = 0; 
  178.  
  179.  
  180.             hRes = ((CSdiDialog*)GetParent())->m_pRegMets->get_Application(&m_pIApp);
  181.             CHECK_HRESULT(hRes)
  182.             
  183.             hRes = m_pIApp->get_Drawings(&m_pDrawCol);
  184.             CHECK_HRESULT(hRes)
  185.  
  186.             hRes = m_pDrawCol->get_Count(&nDrawings);
  187.             CHECK_HRESULT(hRes)
  188.             
  189.             varItem = nDrawings - 1L;
  190.             
  191.             hRes = m_pDrawCol->get_Item(&varItem, &m_pPreviewDrawing);
  192.  
  193.             hRes = m_pPreviewDrawing->get_Views(&m_pViewsCol);
  194.             
  195. //            long nViews = 0;
  196.             
  197.             HWND hWnd = ::GetDlgItem(GetParent()->m_hWnd, IDC_PREVIEW);
  198.             COleVariant varHwnd((long)hWnd);
  199.  
  200. // Create new view to just created drawing and assign to this view HWND of IDC_PREVIEW (Preview window)
  201.             hRes = m_pViewsCol->Add(&varHwnd, &varOptional, &m_pPreviewView );
  202.             CHECK_HRESULT(hRes)
  203.  
  204.  
  205.  
  206. // SetUp just created view
  207.             hRes = m_pPreviewView->put_Update(FALSE);
  208.             CHECK_HRESULT(hRes)
  209.             
  210.  
  211.             hRes = m_pPreviewView->put_MappingMode(MM_TEXT);
  212.             CHECK_HRESULT(hRes)
  213.  
  214.             hRes = m_pPreviewView->put_FixedAspectRatio(TRUE);
  215.             CHECK_HRESULT(hRes)
  216.     
  217.             hRes = m_pPreviewView->put_Margins(FALSE);
  218.             CHECK_HRESULT(hRes)
  219.  
  220.             CPreviewWnd *pPreview = (CPreviewWnd *) (((CSdiDialog*)GetParent())->GetDlgItem(IDC_PREVIEW));
  221.  
  222.  
  223.             CRect rcBounds;
  224.             pPreview->GetClientRect(&rcBounds);
  225.  
  226.             hRes = m_pPreviewView->put_ScreenLeft(rcBounds.left);
  227.             CHECK_HRESULT(hRes)
  228.  
  229.             hRes = m_pPreviewView->put_ScreenTop(rcBounds.top);
  230.             CHECK_HRESULT(hRes)
  231.  
  232.             hRes = m_pPreviewView->put_ScreenWidth(rcBounds.Width());
  233.             CHECK_HRESULT(hRes)
  234.  
  235.             hRes = m_pPreviewView->put_ScreenHeight(rcBounds.Height());
  236.             CHECK_HRESULT(hRes)
  237.  
  238.             hRes = m_pPreviewView->ZoomToExtents();
  239.             CHECK_HRESULT(hRes)
  240.  
  241.             m_FirstStart = FALSE;
  242.         }
  243.     }
  244.     catch (...)
  245.     {
  246.         TRACE_EXCEPTION("CPreviewWnd::CreatePreview")
  247.     }
  248. }
  249.  
  250.  
  251.  
  252. void CPreviewWnd::Clear()
  253. {
  254.     if (m_pPreviewGraphic != NULL)
  255.     {
  256.         m_pPreviewGraphic->put_Deleted(TRUE);
  257.         m_pPreviewGraphic->Release();
  258.         m_pPreviewGraphic = NULL;
  259.     }
  260.     
  261.     Invalidate();
  262. }
  263.  
  264. void CPreviewWnd::ClearAll()
  265. {
  266.     if (m_pPreviewGraphic != NULL)
  267.     {
  268.         m_pPreviewGraphic->put_Deleted(TRUE);
  269.         m_pPreviewGraphic->Release();
  270.         m_pPreviewGraphic = NULL;
  271.     }
  272.     if (m_pPreviewGrs != NULL)
  273.     {
  274.         m_pPreviewGrs->Release();
  275.         m_pPreviewGrs = NULL;
  276.  
  277.     }
  278.     if (m_pPreviewView != NULL)
  279.     {
  280.         m_pPreviewView->Release();
  281.         m_pPreviewView = NULL;
  282.     
  283.     }
  284.     if (m_pViewsCol != NULL)
  285.     {
  286.         m_pViewsCol->Release();
  287.         m_pViewsCol = NULL;
  288.  
  289.     }
  290.     if (m_pPreviewDrawing != NULL)
  291.     {
  292.         COleVariant varOptional(varMissing);
  293.         m_pPreviewDrawing->Close(varOptional, varOptional, varOptional);
  294.         m_pPreviewDrawing->Release();
  295.         m_pPreviewDrawing = NULL;
  296.  
  297.     }
  298.  
  299.     if (m_pDrawCol != NULL)
  300.     {
  301.         m_pDrawCol->Release();
  302.         m_pDrawCol = NULL;
  303.     
  304.     }
  305.  
  306.     if (m_pIApp != NULL)
  307.     {
  308.         m_pIApp->Release();
  309.         m_pIApp = NULL;
  310.         
  311.     }
  312.     m_FirstStart = TRUE;
  313.  
  314. }
  315.  
  316. void CPreviewWnd::DoPreview()
  317. {
  318.     HRESULT hRes;
  319.     COleVariant varType((const long)imsiGroup);
  320.     COleVariant varOptional(varMissing);
  321.     IDrawing *pIDwg = NULL;
  322.     IApplication *pIApp = NULL;
  323.     Properties *pProps = NULL;
  324.     Property *pProp = NULL;
  325.     Properties *pProps1 = NULL;
  326.     Property *pProp1 = NULL;
  327.     COleVariant varVal;//(1L);
  328.     COleVariant flags = (const long)GF_ALL;
  329.     try
  330.     {
  331.         // set PreviewDrawing Mode (Model Space or Paper Space) the same like active drawing 
  332.         // imsiModelSpace = 1, imsiPaperSpace = 0
  333.         hRes = ((CSdiDialog*)GetParent())->m_pRegMets->get_Application(&pIApp);
  334.         CHECK_HRESULT(hRes)
  335.         
  336.         hRes = pIApp->get_ActiveDrawing(&pIDwg);
  337.         CHECK_HRESULT(hRes)
  338. //
  339.         CString cstrName("TileMode");//# Non-localizable string#
  340.         BSTR bstrName = cstrName.AllocSysString();
  341.         
  342.         VARIANT varItem1;
  343.         ::VariantInit(&varItem1);
  344.         varItem1.vt = VT_BSTR;
  345.         varItem1.bstrVal = bstrName;
  346.  
  347.         // get properties of current active  drawing
  348.         hRes = pIDwg->get_Properties(&pProps);
  349.         CHECK_HRESULT(hRes)
  350.  
  351.         hRes = pProps->get_Item(&varItem1, &pProp);
  352.         CHECK_HRESULT(hRes)
  353.         
  354.         hRes = pProp->get_Value(NULL, varVal);
  355.         CHECK_HRESULT(hRes)
  356.  
  357.         // get properties of Preview drawing        
  358.         hRes = m_pPreviewDrawing->get_Properties(&pProps1);
  359.         CHECK_HRESULT(hRes)
  360.  
  361.         hRes = pProps1->get_Item(&varItem1, &pProp1);
  362.         CHECK_HRESULT(hRes)
  363.         
  364.         if (varVal.lVal == imsiModelSpace)  
  365.         {
  366.             hRes = pProp1->put_Value(NULL, varVal);
  367.             CHECK_HRESULT(hRes)
  368.         }
  369.         else 
  370.         {
  371.             hRes = pProp1->put_Value(NULL, varVal);
  372.             CHECK_HRESULT(hRes)
  373.         }
  374.         Invalidate();
  375.         
  376.         ::VariantClear(&varItem1);
  377.         
  378.         CExternalObjectsList *pList = (CExternalObjectsList *) (GetParent()->GetDlgItem(IDC_EXTOBJECTSLIST));
  379.  
  380.         int index = pList->GetCurSel();
  381.  
  382.         pList->GetText(index, m_cstrSelected);
  383.  
  384.         COleVariant varRegen(m_cstrSelected);
  385.         hRes = m_pPreviewDrawing->get_Graphics(&m_pPreviewGrs);
  386.         CHECK_HRESULT(hRes)
  387.  
  388.         hRes = m_pPreviewGrs->Clear(&flags);
  389.  
  390.         hRes = m_pPreviewGrs->Add(&varRegen,
  391.                          &varRegen,
  392.                          &varOptional,
  393.                          &varOptional,
  394.                          &varOptional,
  395.                          &varOptional,
  396.                          &m_pPreviewGraphic);                    
  397.         
  398.         CHECK_HRESULT(hRes)
  399.  
  400.         hRes = m_pPreviewView->ZoomToExtents();
  401.         CHECK_HRESULT(hRes)
  402.     }
  403.     catch (...)
  404.     {
  405.         TRACE_EXCEPTION("CPreviewWnd::DoPreview")
  406.     }
  407.  
  408.     if (pProp1 != NULL)
  409.         pProp1->Release();
  410.  
  411.     if (pProps1 != NULL)
  412.         pProps1->Release();
  413.  
  414.     if (pProp != NULL)
  415.         pProp->Release();
  416.  
  417.     if (pProps != NULL)
  418.         pProps->Release();
  419.  
  420.     if (pIDwg != NULL)
  421.         pIDwg->Release();
  422. }
  423.